var pageNum;
$(function () {

    initLayerDate("#startDate",true);
    queryAjaxKeyWordList(1);
    $('body').on('click','#submitFrom,#querySubmit',function () {
        //点击搜索查询从第一页开始查询
        queryAjaxKeyWordList(1);
    }).on('click','#deleteBatch',function () {//批量删除
        var obj = $('td input.i-checks:checked');
        var keyWordIds = obtainIds(obj);//获取勾选ids
        if(!$.isEmptyObject(keyWordIds)){
            icModale(2, '确认删除架选中的敏感词么', 3, function () {
                updateStatusByKeyWordIds(keyWordIds,obj);
            })
        }
    }).on('click','.deleteKeyWord',function () {//单个删除
        var keyWordIds = $(this).data('id');
        var obj = $(this);
        icModale(2, '确认删除架选中的敏感词么', 3, function () {
            updateStatusByKeyWordIds(keyWordIds,obj);
        })
    }).on('click','#importKeyWord',function () {//批量上传
        uploadExcel();
    })
    saveKeyWord();
    //判断勾选ID是否为空
    function obtainIds(obj) {
        if(obj.size() == 0){
            icModale(0,"请选择要批量操作的数据");
            return null;
        }else {
            var keyWordIds = "";
            obj.each(function (index, em) {
                keyWordIds += em.value + ",";
            })
            keyWordIds = keyWordIds.substr(0,keyWordIds.length-1);
            return keyWordIds;
        }
    }

    /**
     * 获取角色列表页面
     * @param pageNo 当前页码
     */
    function queryAjaxKeyWordList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/op/keyword/ajaxQueryKeywordList',//请求URL
            contextId: 'keyWord-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: queryAjaxKeyWordList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'select-all-keyWord', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#keyWordCount").html(total);
            }
        };
        ajaxGetListData(obj)
    }

    /**
     * 批量删除
     * @param userIds
     * @param obj
     * @param isavalible
     */
    function updateStatusByKeyWordIds(keyWordIds,obj) {
        $.ajax({
            url: baseloaction + "/admin/op/keyword/deleteKeyword",
            data: {"keyWordIds": keyWordIds},
            type: "post",
            dataType: "json",
            success: function (result) {
                icModale(0, result.message);
                if (result.success==true) {
                    obj.parents('tr').remove();
                    var keyWordCount = $("#keyWordCount").html();
                    $("#keyWordCount").html((parseInt(keyWordCount) - obj.size()));
                    queryAjaxKeyWordList(pageNum);

                }
            },
            error: function (error) {
                icModale(0, "系统繁忙,请稍后重试");
            }
        })
    }

    function saveKeyWord() {
        //表格中单个字段的修改
        $('body').on('click', '#saveKeyWord', function() {
            parent.layer.open({
                type: 1,
                area: ['380px'],
                skin: 'layui-layer-demo',
                title: '添加敏感词',
                content: '<div class="m-t m-r m-l m-b"><input class="form-control" id="keyWord" name="word" type="text"></div>',
                btn: '确定',
                yes: function(index, layero) {
                    var keyWord = layero.find('#keyWord').val();
                    $.ajax({
                        url:baseloaction+"/admin/op/keyword/saveKeyword",
                        type:"post",
                        data:{"word":keyWord},
                        dataType:"json",
                        success:function (result) {
                            if (result.success == true) {
                                /**
                                 *   type: 类型：0，1，2。分别为：提示层，弹出层，询问层
                                 *   icon: 图标：0，1，2，3。分别为：警告，正确，错误，疑问
                                 *   content: 文本内容1
                                 *   callback: 按钮回调
                                 *   icModale(type, content, icon, callback)
                                 */
                                icModale(0,result.message);
                                parent.layer.close(index);
                                window.location.href = baseloaction + "/admin/op/keyword/queryKeywordList";
                            } else {
                                icModale(0,result.message);
                            }
                        }
                    });
                }
            });
        });
    }

    function uploadExcel() {

        initUpdateFileWin({
            fileType: 'xls',//文件类型
            multiple: false,//是否允许上传多个文件
            cut: false, //是否允许剪切（fileType='img'）起效
            paramDirName: 'import',//文件上传的目录
            url: baseloaction + "/admin/op/keyword/importKeyWord",
            callback: 'uploadKeyWordExcel'//上传成功后的回调
        });
    }


});
function uploadKeyWordExcel(data) {
    icModale(0, '上传成功');
    window.location.href = baseloaction+"/admin/op/keyword/queryKeywordList"
}

